/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Downloader;

import CrlException.CrlException;
import java.io.*;
import java.net.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 *
 * @author CUHUUHOANG
 */
public class HTTPDownloader {

    public String DownloadHTML(String url) throws CrlException {
        String output = "";
        try {

//            =========================
            if (!Config.ConfigSetting.useProxy) {
                URL myUrl = new URL(url);
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(
                        myUrl.openStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    output += line;
                }
                in.close();

            } else //===========proxy
            {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy", 8080));
                URL myUrl = new URL(url);
                HttpURLConnection uc = (HttpURLConnection) myUrl.openConnection(proxy);
                uc.connect();

                String line;
                StringBuffer tmp = new StringBuffer();
                BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
                while ((line = in.readLine()) != null) {
                    output += line + "\n";
                }
//=================
            }
            return output;
        } catch (MalformedURLException ex) {
            throw new CrlException(ex.getMessage());
        } catch (IOException ex) {
            throw new CrlException(ex.getMessage());
        } catch (Exception ex) {
            throw new CrlException(ex.getMessage());
        } finally {
            return output;
        }

    }

    public Document DownloadDoc(String url) throws CrlException {
        Document doc = null;
        try {
            doc = Jsoup.connect(url).timeout(10000).get();
            return doc;
        } catch (Exception ex) {
            throw new CrlException("Can not download page "+url);
        }
    }
}
